package com.aptemo.webetek.client.widgets.teams;

import java.util.ArrayList;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.enums.EEditMode;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.aptemo.webetek.client.widgets.FocusTextArea;
import com.aptemo.webetek.client.widgets.FocusTextBox;
import com.aptemo.webetek.client.widgets.dialogs.DecisionDialog;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class UserGroupResume
		extends Composite implements UserGroupResumePresenter.Display
{

	private static UserGroupUiBinder	uiBinder	= GWT.create(UserGroupUiBinder.class);

	interface UserGroupUiBinder extends UiBinder<Widget, UserGroupResume>
	{
	}

	private MultiWordSuggestOracle	oracle				= new MultiWordSuggestOracle();

	// /** Used by MyUiBinder to instantiate */
	// @UiFactory
	// SuggestBox makeSuggestBox()
	// {
	// return new SuggestBox(oracle);
	// }

	@UiField
	HTML							titleField;

	@UiField
	HTML							infoField;

	@UiField
	FocusTextBox					ownerField;

	@UiField
	Button							saveBtn;

	@UiField
	Button							deleteBtn;

	@UiField
	FocusTextBox					nameField;

	@UiField
	FocusTextArea					descField;

	@UiField
	FocusTextArea					rulesField;

	@UiField
	TextBox							minStake;

	@UiField
	TextBox							maxStake;

	@UiField
	CheckBox						isPublicCheckbox;

	@UiField
	CheckBox						notificationsCheckbox;

	DecisionDialog					deleteConfirmDialog	= new DecisionDialog("Deleting team...");

	public UserGroupResume()
	{
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public HasText getDescField()
	{
		return descField;
	}

	@Override
	public HasText getNameField()
	{
		return nameField;
	}

	@Override
	public HasClickHandlers getSaveBtn()
	{
		return saveBtn;
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public void setEditMode(EEditMode mode)
	{
		switch (mode)
		{
			case EEDIT:
				infoField.setText(Webetek.getConstants().modifyingData());

				// membersList.setVisible(true);
				// scoreBoard.setVisible(true);
				deleteBtn.setVisible(true);
				saveBtn.setVisible(true);
				nameField.setReadOnly(false);
				descField.setReadOnly(false);

				ownerField.removeStyleName("readonly");
				ownerField.setStyleName("gwt-SuggestBox");

				break;
			case EADD:
				// infoField.setText(Webetek.getConstants().addingData());

				deleteBtn.setVisible(false);
				saveBtn.setVisible(true);
				nameField.setReadOnly(false);
				descField.setReadOnly(false);

				ownerField.removeStyleName("readonly");
				ownerField.addStyleName("gwt-SuggestBox");

				break;
			default:
				// infoField.setText("Team information...");

				nameField.setReadOnly(true);
				descField.setReadOnly(true);
				deleteBtn.setVisible(false);
				saveBtn.setVisible(false);

				// membersList.setVisible(false);
				// scoreBoard.setVisible(true);

				// ownerField.removeStyleName("gwt-SuggestBox");
				ownerField.setStyleName("teamReadonly");

				break;
		}
	}

	@Override
	public HasValue<Boolean> getPublicCheckbox()
	{
		return isPublicCheckbox;
	}

	@Override
	public HasText getOwnerField()
	{
		return ownerField;
	}

	@UiHandler("nameField")
	void handleKeyPress(KeyPressEvent event)
	{
		saveBtn.setEnabled(true);
	}

	@UiHandler("descField")
	void handleKeyPress1(KeyPressEvent event)
	{
		saveBtn.setEnabled(true);
	}

	@Override
	public void setPlayersList(ArrayList<UserBean> list)
	{
		// membersList.setText("members (" + list.size() + ")");
		oracle.add(ownerField.getText());
		for (UserBean item : list)
		{
			oracle.add(item.getEmail());
		}
	}

	@Override
	public HasText getTitleField()
	{
		return titleField;
	}

	@UiHandler("deleteBtn")
	void deleteClickHandler(ClickEvent event)
	{
		deleteConfirmDialog.setQuestionString(Webetek.getMessages().deleteTeamQuestion(nameField.getText()));
		deleteConfirmDialog.center();
	}

	@Override
	public HasDecisionHandlers getDeleteConfirmationDialog()
	{
		return deleteConfirmDialog;
	}

	@Override
	public HasText getRulesField()
	{
		return rulesField;
	}

	@Override
	public HasValue<String> getMinStake()
	{
		return minStake;
	}

	@Override
	public HasValue<String> getMaxStake()
	{
		return maxStake;
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

	@Override
	public HasValue<Boolean> getNotificationsCheckbox()
	{
		return notificationsCheckbox;
	}

	@Override
	public String enterValidValue(String value)
	{
		try
		{
			return Webetek.getMessages().enterValidValue(translate(value));
		}
		catch (MissingResourceException e)
		{
			return Webetek.getMessages().enterValidValue(value);
		}
	}

	@Override
	public void setOwnerEmail(String email)
	{
		ownerField.setText(email);
	}

	@UiHandler("nameField")
	void nameValueChangeHandler(ValueChangeEvent<String> event)
	{
		titleField.setText(event.getValue());
	}

	@Override
	public HasText getSaveBtnTitle()
	{
		return saveBtn;
	}

	@Override
	public void hideDeleteConfirmationDialog()
	{
		deleteConfirmDialog.hide();
	}

}
